//
//  SubjectDetailView.m
//  
//
//  Created by Christian Speich on 18.12.07.
//  Copyright 2007-2008 Christian Speich. All rights reserved.
//

#import "SubjectDetailView.h"
#import "SubjectsManager.h"
#import <UIKit/UIPreferencesTableCell.h>
#import <UIKit/UIPreferencesTextTableCell.h>
#import <UIKit/UIKeyboardLayoutQWERTY-UI_QWERTY_AlphabetTransparent.h>
#import "SubjectEditView.h"
#import "NewDeleteRefresh.h"

@implementation SubjectDetailView

- (id) initWithFrame:(struct CGRect)frame andSubjectIndex:(int)i
{
	self = [super initWithFrame:frame];
	if (self != nil) {
		index = i;
		editMode = NO;
		table = [[UIPreferencesTable alloc] 
				 initWithFrame:CGRectMake(0.0f, 0.0f, frame.size.width,
										  frame.size.height)];
		[table setDataSource:self];
		[table setDelegate:self];
		[table reloadData];
		
		[self addSubview:table];
	}
	return self;
}

- (void)reload
{
	[table reloadData];
}

- (int)index
{
	return index;
}

- (void)toggleEdit
{
	NSMutableArray *insertItems = [[NSMutableArray alloc] initWithCapacity:0];
	NSMutableArray *deleteItems = [[NSMutableArray alloc] initWithCapacity:0];
	NSMutableArray *refreshItems = [[NSMutableArray alloc] initWithCapacity:0];
	
	if (editMode) {
		editMode = false;
		[table selectRow:-1 byExtendingSelection:NO withFade:NO];
		[refreshItems addObject:[NSNumber numberWithInt:
								 [table tableRowForRow:1 inGroup:0]]];
		
		if ([[[[SubjectsManager sharedInstance] subjectAtIndex:index]
			  classMarks] count] == 0) {
			[[table visiblePreferencesCellForRow:0 inGroup:1] 
			 setTitle:NSLocalizedString(@"none",
										@"Label for none")];
		}
		else {
			[deleteItems addObject: [[NewDeleteRefresh alloc] 
									 initNewCellWithGroup:1  andLocation:
									 [[[[SubjectsManager sharedInstance] 
										subjectAtIndex:index] classMarks] count]
									 andRange:1]];
		}
									
		if ([[[[SubjectsManager sharedInstance] subjectAtIndex:index] 
			  otherMarks] count] == 0) {
			[[table visiblePreferencesCellForRow:0 inGroup:2] 
			 setTitle:NSLocalizedString(@"none",
										@"Label for none")];
		}
		else {
			[deleteItems addObject:[[NewDeleteRefresh alloc]
									 initNewCellWithGroup:2
									 andLocation:[[[[SubjectsManager 
													 sharedInstance] 
													subjectAtIndex:index] 
												   otherMarks] count]  
									 andRange:1]];
		}
															
		[table insertItems:insertItems deleteItems:deleteItems
		  andReloadIndexes:[[[NewDeleteRefresh alloc] 
							 initRefreshWithArray:refreshItems] autorelease]];
		[table enableRowDeletion:NO animated:YES];
		[[viewManager bar] showLeftButton:nil withStyle:0 
							  rightButton:NSLocalizedString(@"edit",
															@"Label for Edit") 
								withStyle:0];	
	}
	else {
		editMode = true;

		[refreshItems addObject:[NSNumber numberWithInt:
								 [table tableRowForRow:1 inGroup:0]]];
		
		if ([[[[SubjectsManager sharedInstance] subjectAtIndex:index]
			  classMarks] count] == 0) {
			UIPreferencesTableCell *cell = [table 
											visiblePreferencesCellForRow:0 
											inGroup:1];
			[cell setTitle:NSLocalizedString(@"newClassTest",
											 @"Label for new class Test")];
			newClassTestRow = [table tableRowForRow:0 inGroup:1];	
		}
		else {
			[insertItems addObject:[[NewDeleteRefresh alloc] 
									 initNewCellWithGroup:1 
									 andLocation:[[[[SubjectsManager 
													 sharedInstance] 
													subjectAtIndex:index] 
												   classMarks] count]  
									 andRange:1]];
		}
									
		if ([[[[SubjectsManager sharedInstance] subjectAtIndex:index] 
			  otherMarks] count] == 0) {
			UIPreferencesTableCell *cell = [table 
											visiblePreferencesCellForRow:0 
											inGroup:2];
			[cell setTitle:NSLocalizedString(@"newTest",
											 @"Label for new Test")];
			newTestRow = [table tableRowForRow:0 inGroup:2];	
		}
		else {
			[insertItems addObject:[[NewDeleteRefresh alloc]
									 initNewCellWithGroup:2
									 andLocation:[[[[SubjectsManager 
													 sharedInstance] 
													subjectAtIndex:index] 
												   otherMarks] count]  
									 andRange:1]];
		}
		
		[table insertItems:insertItems deleteItems:deleteItems 
		  andReloadIndexes:[[[NewDeleteRefresh alloc] 
							 initRefreshWithArray:refreshItems] autorelease]];
		[table enableRowDeletion:YES animated:YES];
		[[viewManager bar] showLeftButton:nil withStyle:0 
							  rightButton:NSLocalizedString(@"done",
															@"Label for done") 
								withStyle:3];	
	}
}

- (int)numberOfGroupsInPreferencesTable:(UIPreferencesTable *)table
{
	return 3;
}

- (void)updateAverage
{
	[[table visiblePreferencesCellForRow:0 inGroup:0] 
	 setValue:[NSString stringWithFormat:@"%1.1f", 
			   [[[SubjectsManager sharedInstance] 
				 subjectAtIndex:index] average]]];
}

- (int)preferencesTable:(UIPreferencesTable *)_table 
	numberOfRowsInGroup:(int)group
{
	int classTestGroups = 1;
	int testGroups = 2;
	
	if (group == 0) {
		return 2;
	}
	else if (group == classTestGroups) {
		if (editMode) {
			return [[[[SubjectsManager sharedInstance] subjectAtIndex:index] 
					 classMarks] count] +1;
		}
		else {
			if ([[[[SubjectsManager sharedInstance] subjectAtIndex:index] 
				  classMarks] count] == 0) {
				return 1;
			}
			else {
				return [[[[SubjectsManager sharedInstance] subjectAtIndex:index] 
					 classMarks] count];
			}
		}
	}
	else if (group == testGroups) {
		if (editMode) {
			return [[[[SubjectsManager sharedInstance] subjectAtIndex:index] 
					 otherMarks] count] +1;
		}
		else {
			if ([[[[SubjectsManager sharedInstance] subjectAtIndex:index] 
				  otherMarks] count] == 0) {
				return 1;
			}
			else {
				return [[[[SubjectsManager sharedInstance] subjectAtIndex:index] 
						 otherMarks] count];
			}
		}
	}
	
}

- (NSString *) preferencesTable:(UIPreferencesTable *) table titleForGroup:(int) group {
	if( group == 0 )
		return @"Allgemeines";
	if( group == 1 )
		return NSLocalizedString(@"classTest",
								@"Label for test");
	if( group == 2 )
		return NSLocalizedString(@"test",
								 @"Label for test");
	return nil;
}

- (UIPreferencesTableCell *)preferencesTable:(UIPreferencesTable *)_table 
								  cellForRow:(int)row inGroup:(int)group
{	
	if (group == 0) {
		if (row == 0) {
			UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] 
											init];
			[cell setTitle:NSLocalizedString(@"average",
											 @"Label for average")];
			[cell setValue:[NSString stringWithFormat:@"%1.1f", 
							[[[SubjectsManager sharedInstance] 
							  subjectAtIndex:index] average]]];
			return [cell autorelease];
		}
		else if (row == 1) {
			if (NO) {
				UIPreferencesTextTableCell *cell = [[UIPreferencesTextTableCell 
													 alloc] init];
				[cell setTitle:NSLocalizedString(@"teacher",
												 @"Label for teacher")];
				[cell setValue:[[[SubjectsManager sharedInstance] 
								 subjectAtIndex:index] teacher]];
				return [cell autorelease];
			} else {
				UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc]
												init];
				[cell setTitle:NSLocalizedString(@"teacher",
												 @"Label for teacher")];
				[cell setValue:[[[SubjectsManager sharedInstance] 
								 subjectAtIndex:index] teacher]];
				return [cell autorelease];
			}
		}
	}
	else if (group == 1) {
		if (row >=[[[[SubjectsManager sharedInstance] subjectAtIndex:index] 
					classMarks] count] && editMode) {
			UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] 
											init];
			[cell setTitle:NSLocalizedString(@"newClassTest",
											 @"Label for new Class Test")];
			newClassTestRow = [table tableRowForRow:row inGroup:group];	
			return [cell autorelease];
		}
		else if (0 < [[[[SubjectsManager sharedInstance] subjectAtIndex:index] 
						 classMarks] count]) {
			UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] 
											init];
			[cell setTitle:[NSString stringWithFormat:@"%i",
							[[[[[SubjectsManager sharedInstance] 
								subjectAtIndex:index] classMarks]
							  objectAtIndex:row] mark]]];
			[cell setValue:[NSString stringWithFormat:@"%1.0f%%",
							[[[[[SubjectsManager sharedInstance] 
								subjectAtIndex:index] classMarks] 
							  objectAtIndex:row] valency]*100]];
			return [cell autorelease];
		}
	}
	else if (group == 2) {
		if (row >= [[[[SubjectsManager sharedInstance] subjectAtIndex:index] 
					 otherMarks] count] && editMode) {
			UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] 
											init];
			[cell setTitle:NSLocalizedString(@"newTest",
											 @"Label for new Test")];
			newTestRow = [table tableRowForRow:row inGroup:group];	
			return [cell autorelease];
		}
		else  if (0 < [[[[SubjectsManager sharedInstance] subjectAtIndex:index] 
						otherMarks] count]){
			UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc]
											init];
			[cell setTitle:[NSString stringWithFormat:@"%i",
							[[[[[SubjectsManager sharedInstance] 
								subjectAtIndex:index] otherMarks] 
							  objectAtIndex:row] mark]]];
			return [cell autorelease];
		}
	}

	UIPreferencesTableCell *cell = [[UIPreferencesTableCell alloc] init];
	[cell setTitle:NSLocalizedString(@"none",
									 @"Label for none")];
	return [cell autorelease];
}

- (void)tableRowSelected:(NSNotification*)notification
{
	if (editMode && [table selectedRow] == newClassTestRow) {
		[viewManager addView: [[SubjectEditView alloc] 
							   initNewClassTestWithSubjectIndex:[self index] 
							   andFrame: [self frame]]];
	}
	else if (editMode && [table selectedRow] == newTestRow) {
		[viewManager addView: [[SubjectEditView alloc] 
							   initNewTestWithSubjectIndex:[self index] 
							   andFrame: [self frame]]];
	}
}

- (BOOL)table:(UITable*)table canDeleteRow:( int)row {
	if (row < 3 || row == newTestRow || row == newClassTestRow) {
		return NO;
	}
	return YES;
}

- (BOOL)table:(UITable*)table canInsertAtRow:(int)row
{
	if (row == newTestRow || row == newClassTestRow) {
		return YES;
	}
	return NO;
}

- (void)table:(UITable*)_table deleteRow:(int)_row {
	int group;
	int row;
	Mark* mark = nil;
	
	[table getGroup:&group row:&row forTableRow:_row];
	if (group == 1) {
		mark = [[[[SubjectsManager sharedInstance] 
				  subjectAtIndex:index] classMarks]
				objectAtIndex:row];
		newClassTestRow--;
	}
	else {
		mark = [[[[SubjectsManager sharedInstance] 
				 subjectAtIndex:index] otherMarks]
			   objectAtIndex:row];
		newTestRow--;
	}
	
	[[[SubjectsManager sharedInstance] subjectAtIndex:index] removeMark:mark];

	[self updateAverage];
}

- (void)becomesManager:(id)manager
{
	viewManager = (ViewManager*)manager;
}

- (void)becomesBack
{
	[self reload];
}

- (UINavigationItem*)NavigationItem
{
	return [[UINavigationItem alloc] 
			initWithTitle:[[[SubjectsManager sharedInstance] 
							subjectAtIndex:index] name]];
}

- (void)navigationBar:(UINavigationBar*)_bar buttonClicked:(int)button
{
	if (button == 1) {
		[viewManager back];
		return;
	}
	else {
		[self toggleEdit];
	}
}

- (void)showBarButtons
{
	if (editMode) {
		[[viewManager bar] showLeftButton:nil withStyle:2 
							  rightButton:NSLocalizedString(@"done",
															@"Label for done") 
								withStyle:3];
	}
	else {
		[[viewManager bar] showButtonsWithLeft:nil 
										 right:
		 NSLocalizedString(@"edit",
						   @"Label for Edit") leftBack:NO];
	}
	
}

- (BOOL) table:(UITable*)_table canSelectRow:(int)row
{
	if (row == newTestRow || row == newClassTestRow) {
		return YES;
	}
	return NO;
}

@end
